home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / UPDATE10.TXT < prev   
Text File  |  1992-12-08  |  35KB  |  1,117 lines

  1.  
  2.  
  3.  
  4.               The Purdue Compiler Construction Tool Set
  5.  
  6.                          Version 1.06 Update
  7.  
  8.                            December 1, 1992
  9.  
  10.  
  11. 1.  Enhancements
  12.  
  13.      This section describes the update of PCCTS Version 1.00  to  Ver-
  14. sion  1.06  (1.01-1.05  were  internal releases).  The list of changes
  15. here is not complete since many "little" fixes  were  made  (actually,
  16. the predicated parsing feature is major).  Here, we concentrate on the
  17. enhancements to the system; file BUGS100 contains a list  of  the  bug
  18. fixes  incorporated  in  the 1.06 release.  In addition, note that the
  19. manual has not been updated and will  not  be  until  the  next  major
  20. release (or until we get around to it).
  21.  
  22. 1.1.  Predicated Parsing - ALPHA Version
  23.  
  24.      Normally, parsing is  a  function  of  syntax  alone.   Semantics
  25. (meaning/value  of  the input) are not taken into account when parsing
  26. decisions are made-context-free parsing.  Typically,  languages,  such
  27. as  C++, have some rules which require the lexical analyzer to consult
  28. the symbol table  to  determine  what  token  (e.g.  classname  verses
  29. enumerated name verses typedef name) is given to the parser.  However,
  30. lexical analyzers have no context information except the current token
  31. of  lookahead and must be coerced via flags to yield the various token
  32. types.  Ad hoc approaches become increasingly difficult  to  implement
  33. as  the number of ambiguities (due to lack of semantic information) in
  34. a grammar rises.
  35.  
  36.      Context dictates the scope of variables in programming languages.
  37. Because  only  the parser has context information, it is reasonable to
  38. assume that the parser is the correct place to maintain and access the
  39. symbol  table;  there  are  other situtations in which it is useful to
  40. have context direct parsing.  Unfortunately, most parser generators do
  41. not  allow  grammar  actions to influence the parse; i.e. parsing is a
  42. function of syntax alone.  Because PCCTS  generates  recursive-descent
  43. LL(k) parsers in C, allowing user-defined C actions to influence pars-
  44. ing is a straightforward and natural addition to the PCCTS description
  45. meta-language.    Although  bottom-up  parsing  strategies  can  allow
  46. actions to direct parsing, bottom-up parser have  much  less  semantic
  47. information; i.e. LR and LALR techniques allow only S-attributed gram-
  48. mars whereas LL allows L-attributed grammars (for example,  LL  always
  49. knows  which set of rules it is currently trying to match whereas LALR
  50. does not, in general; see your favorite book on parsing theory).
  51.  
  52.      In this section, we introduce a  rudimentary  version  of  predi-
  53. cates,  with  a  full implementation to appear in future releases.  To
  54. support context-sensitive parsing PCCTS 1.06 allows a new action  type
  55. called predicate (-pr must be used), which is denoted:
  56.  
  57.  
  58.  
  59.  
  60.  
  61.                                                                 Page 1
  62.  
  63.                                                                  PCCTS
  64.  
  65.  
  66.  
  67. <<predicate>>?
  68.  
  69.  
  70. or, optionally,
  71.  
  72.  
  73. <<predicate>>?[fail_action]
  74.  
  75.  
  76. where the second notation "passes" an argument of  an  action  to  the
  77. predicate  operator  "?", which is executed upon predicate failure.  A
  78. predicate is a C action that evaluates to  either  true  (success)  or
  79. false  (failure)  and can be used for both semantic validation and for
  80. disambiguating syntactic conflicts in the underlying grammar.
  81.  
  82.      Validation predicates are used as simple tests and are  function-
  83. ally equivalent to the following normal action:
  84.  
  85.  
  86. if ( !predicate ) {error message; exit rule}
  87.  
  88.  
  89. or, when a fail action is present:
  90.  
  91.  
  92. if ( !predicate ) {fail_action}
  93.  
  94.  
  95. For example,
  96.  
  97.  
  98. a   :   A <<pred>>? B
  99.     ;
  100.  
  101.  
  102. Matches A, evaluates pred, and matches B if pred is true else an error
  103. is  reported  (i.e.  "failed  predicate  'pred'") and rule a is exited
  104. before matching B.  To generate a more useful error message, the  user
  105. may specify a fail action:
  106.  
  107.  
  108. a   :   A <<pred>>?[fail] B
  109.     ;
  110.  
  111.  
  112. which executes fail when pred evaluates to false.
  113.  
  114.      A predicate which validates a production can, at the  same  time,
  115. be  used  to disambiguate a syntactically ambiguous grammar structure.
  116. To be a candidate, a predicate must appear on the extreme left edge of
  117. a  production.   Disambiguating predicates are used in the alternative
  118. prediction expressions to enable or disable certain  alternative  pro-
  119. ductions   of  a  block  (rule  or  subrule).   Parsing  in  this  new
  120.  
  121.  
  122.  
  123.                                                                 Page 2
  124.  
  125.                                                                  PCCTS
  126.  
  127.  
  128. environment can be viewed in this way:
  129.  
  130. step 1: Disable invalid productions
  131.      An invalid production is a production whose disambiguating predi-
  132.      cate evaluates to false.
  133.  
  134. step 2: Parse as normal block
  135.      Once a list of valid productions has been found, they are  parsed
  136.      as a normal rule or subrule.
  137.  
  138. Essentially, disambiguating predicates "gate" alternative  productions
  139. in  and  out  depending on their "applicability".  Productions without
  140. predicates have an implied  predicate  of  <<TRUE>>?;  i.e.  they  are
  141. always valid.
  142.  
  143.      A single predicate can be used as both a validation and a  disam-
  144. biguating  predicate-ANTLR  determines which way to use them automati-
  145. cally.  The role taken by a predicate depends on its location  in  the
  146. grammar.   Predicates are all considered validation predicates as they
  147. must always evaluate to true for parsing of the  enclosing  production
  148. to  continue.   However,  when  a grammatical ambiguity is discovered,
  149. ANTLR searches for visible predicates to disambiguate  the  construct.
  150. A  visible  predicate  is  one  which can be evaluated in a production
  151. prediction expression without consuming a token or executing an action
  152. (except  initialization  actions);  e.g. all disambiguating predicates
  153. appear on the left edge of some production.  Fail actions are not used
  154. in  prediction  expressions because a failed predicate disables a pro-
  155. duction; it does not report a syntax  error  unless  no  other  viable
  156. predicates  are present; a viable production is one which is predicted
  157. by the lookahead.  The action of moving a predicate into a  prediction
  158. expression is called hoisting; currently, at most one predicate can be
  159. hoisted per ambiguous production.  In general, predicates may only  be
  160. a function of:
  161.  
  162. User variables
  163.      This is dangerous as hoisting may evaluate  the  predicate  in  a
  164.      scope  where  the  variable(s)  does  not  exist; e.g. using rule
  165.      parameters can be a problem if a predicate that  references  them
  166.      is hoisted outside of the rule.
  167.  
  168. Attributes
  169.      Only attributes in the left context can be used; i.e.  attributes
  170.      of rules/tokens created before the predicate is evaluated.
  171.  
  172. Lookahead
  173.      The  next  k  tokens  of  lookahead  can  be  tested  via  LA(i),
  174.      LATEXT(i).
  175.  
  176. Also, predicates may not have side-effects (user  must  undo  anything
  177. that  was  changed if they do; in other words, they must "backtrack").
  178. See the Future Work section  for  information  regarding  the  use  of
  179. predicates and extremely large lookahead buffers.
  180.  
  181.      Consider the following simple grammar:
  182.  
  183.  
  184.  
  185.                                                                 Page 3
  186.  
  187.                                                                  PCCTS
  188.  
  189.  
  190.  
  191. a   :   <<pred1>>? A B
  192.     |   <<pred2>>? A C
  193.     ;
  194.  
  195.  
  196. This grammar is LL(2) (-k 2) and ANTLR would not report an  ambiguity.
  197. However,  it  is LL(1) ambiguous (-k 1) and antlr t.g would generate a
  198. warning message:
  199.  
  200.  
  201. t.g, line 1: warning: alts 1 and 2 of rule ambiguous upon { A }
  202.  
  203.  
  204. Now, if we allow predicates to be used with antlr -pr t.g, then  ANTLR
  205. finds that both predicates are visible and can be used to disambiguate
  206. the production; it is up to the user to ensure that the predicates do,
  207. in  fact, disambiguate the predicate.  ANTLR would generate code simi-
  208. lar to the following:
  209.  
  210.  
  211. a()
  212. {
  213.     if ( pred1 && LA(1)==A ) {
  214.     }
  215.     else if ( pred2 && LA(1)==A ) {
  216.     }
  217.     else error;
  218. }
  219.  
  220.  
  221. The following grammars are syntactically and  semantically  equivalent
  222. to the above grammar, but the predicates are not hoisted trivially:
  223.  
  224.  
  225. a   :   ( <<pred1>>? A B )
  226.     |   <<pred2>>? A C
  227.     ;
  228.  
  229.  
  230. ANTLR looks inside the subrule of production one and finds that  pred1
  231. can  be  evaluated  before  executing  an action and before matching a
  232. token; it is hoisted for use in the prediction expression of  rule  a.
  233. Predicates can be hoisted from other rules as well:
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.                                                                 Page 4
  248.  
  249.                                                                  PCCTS
  250.  
  251.  
  252.  
  253. a   :   b
  254.     |   c
  255.     ;
  256.  
  257. b   :   <<pred1>>? A B
  258.     ;
  259.  
  260. c   :   <<pred2>>? A C
  261.     ;
  262.  
  263.  
  264. Rule a would still have the  same  production  prediction  expression.
  265. The  following grammar does not have predicates that can be hoisted to
  266. disambiguate the prediction:
  267.  
  268.  
  269. a   :   A <<pred1>>? B             /* cannot hoist past token */
  270.     |   <<action>> <<pred2>>? A C  /* cannot hoist past action */
  271.     ;
  272.  
  273.  
  274.      Now, consider how a simplified version  of  K&R  C  variable  and
  275. function declarations could be specified:
  276.  
  277.  
  278. decl:   /* variable definition, function DECLARATION/DEFINITION */
  279.         type declarator ( func_body | ";" )
  280.     |   /* function DEFINITION */
  281.         declarator func_body
  282.     ;
  283.  
  284. type:   "int"
  285.     |   "float"
  286.     |   WORD
  287.     ;
  288.  
  289. declarator
  290.     :   WORD { "\(" args "\)" }
  291.     ;
  292.  
  293. func_body
  294.     :   ( decl )* "\{" (decl)* (stat)* "\}"
  295.     ;
  296.  
  297.  
  298. unfortunately, rule decl is totally ambiguous in the LL(1) sense  (and
  299. in  the  LL(k)  sense)  since WORD can begin both alternatives of rule
  300. decl.  Increasing the lookahead to two tokens does  not  help  as  the
  301. alternatives are ambiguous upon WORDWORD.  Alternative one could match
  302. my_type var and alternative two could match func my_type;  hence,  the
  303. second  alternative  matches  at  least one invalid sentence.  This is
  304. because the ( decl )* subrule of rule func_body, which would match the
  305. second  WORD  of WORD WORD, does not know if an argument list was seen
  306.  
  307.  
  308.  
  309.                                                                 Page 5
  310.  
  311.                                                                  PCCTS
  312.  
  313.  
  314. previously or not.  The question  of  whether  to  match  a  parameter
  315. definition  list  after  a  declarator  is context-sensitive because a
  316. function header may or may not have been seen immediately beforehand.
  317.  
  318.      One way in which this subset could be recognized is to  recognize
  319. a  large superset of the language and then, using actions, verify that
  320. the input was valid:
  321.  
  322.  
  323. decl:   {type} declarator ( func_body | ";" )
  324.     ;
  325.  
  326.  
  327. But, jamming everything together makes action introduction  more  com-
  328. plicated.   It  is  better  to specify the input language exactly with
  329. predicates.
  330.  
  331.      To overcome the ambiguity in rule decl and to ensure that only  a
  332. function  declarator is followed by a parameter definition, we augment
  333. the grammar as follows:
  334.  
  335.  
  336. decl:   <<int is_func;>>
  337.  
  338.         /* variable definition, function DECLARATION/DEFINITION */
  339.         type declarator > [is_func]
  340.         (   <<is_func>>?[printf("warning: declarator not func\n");]
  341.             func_body
  342.         |   ";"
  343.         )
  344.  
  345.     |   /* function DEFINITION */
  346.         declarator > [is_func]
  347.         <<is_func>>?[printf("warning: expecting func header not %s\n", LATEXT(1));]
  348.         func_body
  349.     ;
  350.  
  351. type:   "int"
  352.     |   "float"
  353.     |   <<LA(1)==WORD ? isTYPE(LATEXT(1)) : 1>>?
  354.         WORD
  355.     ;
  356.  
  357. declarator > [int is_func]
  358.     :   <<LA(1)==WORD ? !isTYPE(LATEXT(1)) : 1>>?
  359.         WORD ( "\(" <<$is_func=1;>> args "\)" | <<$is_func=0;>> )
  360.     ;
  361.  
  362. func_body
  363.     :   ( decl )* "\{" (decl)* (stat)* "\}"
  364.     ;
  365.  
  366.  
  367. In rule type, we add a predicate that ensures that, if  the  lookahead
  368.  
  369.  
  370.  
  371.                                                                 Page 6
  372.  
  373.                                                                  PCCTS
  374.  
  375.  
  376. is a WORD, that the WORD is, in fact, a user-defined type; nothing can
  377. be said if the lookahead is something else, so we  return  success  in
  378. that  case; i.e. the predicate does not apply for that lookahead (con-
  379. text).  If rule declarator is called, a failure of the predicate  will
  380. report  a  semantic  error.   In  our  example,  the predicate is also
  381. hoisted for use as a  disambiguating  predicate  in  rule  decl.   The
  382. predicate in rule declarator ensures that, if the lookahead is a WORD,
  383. that the WORD is not a user-defined  type.   As  before,  it  is  also
  384. hoisted  to  rule  decl  to disambiguate the second production of that
  385. rule.  Both productions of rule decl have predicates that are  hoisted
  386. into  the  prediction expressions for those productions; ANTLR assumes
  387. that the hoisted  predicates  completely  disambiguate  the  decision,
  388. which  is true in our case.  The first predicate physically present in
  389. rule decl ensures that, if a function body is coming up, the  declara-
  390. tor  was  a  function header.  The second predicate in rule decl again
  391. ensures that a function header was seen before trying to match a func-
  392. tion  body.   The  variable is_func is set to the return value of rule
  393. declarator, which is set by two simple actions in declarator.
  394.  
  395.      Currently, the context under which predicates are  evaluated  may
  396. not be correct; i.e. currently ANTLR does not compute the context from
  397. which it hoists a predicate.  For example,
  398.  
  399.  
  400. a   :   b B
  401.     |   (X Y | A C)
  402.     ;
  403.  
  404. b   :   <<pred>>? A
  405.     ;
  406.  
  407.  
  408. The predicate pred will be hoisted for use in disambiguating  rule  a.
  409. However, the predicate will be evaluate regardless of whether A, which
  410. is its context, or X is on the input stream.  It  may  be  invalid  to
  411. apply  the  predicate  when the lookahead is X.  The user may overcome
  412. this by changing pred thus:
  413.  
  414.  
  415. a   :   b B
  416.     |   (X Y | A C)
  417.     ;
  418.  
  419. b   :   <<LA(1)==A ? pred : TRUE>>? A
  420.     ;
  421.  
  422.  
  423. which will ensure that the predicate is only evaluated when A is  seen
  424. on the input stream.
  425.  
  426.      Another problem area lies in that hoisting can  occur  in  situa-
  427. tions that will break the semantics.  For example:
  428.  
  429.  
  430.  
  431.  
  432.  
  433.                                                                 Page 7
  434.  
  435.                                                                  PCCTS
  436.  
  437.  
  438.  
  439. a   :   b[3]
  440.     |   A
  441.     ;
  442.  
  443. b[int i]
  444.     : <<f($i)>>? A
  445.     ;
  446.  
  447.  
  448. The predicate f(i) will be hoisted to rule a where the parameter i  is
  449. not  even defined.  Either do not do this or put a predicate (dummy or
  450. otherwise) between the decision and the predicate you do not  wish  to
  451. be hoisted:
  452.  
  453.  
  454. a   :   <<1>>? b[3]
  455.     |   A
  456.     ;
  457.  
  458. b[int i]
  459.     : <<f($i)>>? A
  460.     ;
  461.  
  462.  
  463.      This is an alpha release feature in that it is not anywhere  near
  464. what  we  want  it  to  do  and  has NOT been thoroughly tested.  User
  465. beware.  We do not guarantee that the syntax of predicates  will  stay
  466. this  way;  however,  the semantics will not change.  We want users to
  467. play with predicates to find new, better or different  ways  of  doing
  468. things.   We really want user feedback on these critters before a full
  469. implementation is developed.  For example, how do you want the  parser
  470. to respond if no valid, viable production is found?
  471.  
  472.  
  473. a   :   <<p1>>? A
  474.     |   <<p2>>? A
  475.     |   B
  476.     ;
  477.  
  478.  
  479. If the input were C, a correct error message would be reported:
  480.  
  481.  
  482. line 1: syntax error at "C" missing { A B }
  483.  
  484.  
  485. This is the correct message because it is truly a syntax,  as  opposed
  486. to  semantic, error.  On the other hand, upon input A, when predicates
  487. p1 and p2 are false, the parser reports the following bizarre message:
  488.  
  489.  
  490. line 1: syntax error at "A"
  491.  
  492.  
  493.  
  494.  
  495.                                                                 Page 8
  496.  
  497.                                                                  PCCTS
  498.  
  499.  
  500. which is not very meaningful.  The fail function, zzFAIL, found  that,
  501. in  fact,  A  is syntactically valid and got confused because an error
  502. was raised; this is because the fail routine has no semantic  informa-
  503. tion.   A  suitable  definition  of  error  reporting in the predicate
  504. environment must be created.
  505.  
  506.      Please send any  suggestions/questions  to  parrt@ecn.purdue.edu,
  507. hankd@ecn.purdue.edu   or   use   the   email   server   mechanism  at
  508. pccts@ecn.purdue.edu (send a Subject: line  of  "question  antlr"  and
  509. fill in the body of the email with your suggestion/question).
  510.  
  511. 1.2.  1.06 Is Written in 1.00
  512.  
  513.      The grammar for the PCCTS meta-language (file  format)  has  been
  514. implemented in Version 1.00, making heavy use of #lexclass directives.
  515. File lexhelp.c has been eliminated due to the superiority of  1.00  to
  516. 1.00B.
  517.  
  518. 1.3.  ANTLR Compiles Under ANSI C
  519.  
  520.      Because of the rewrite of the grammar and some rewrites of  ANTLR
  521. code,  ANTLR now compiles with ANSI compilers without a wimper (except
  522. for two "unknown escape sequence" warnings).  Your mileage may vary.
  523.  
  524. 1.4.  Grammar Files Distributed
  525.  
  526.      antlr.g and dlg_p.g are now included in the  source  distribution
  527. for  1.06.  Note that the 1.00 PCCTS (both ANTLR and DLG) are required
  528. to process these grammar files.  DO NOT DESTROY YOUR OLD COPY OF  1.00
  529. PCCTS (at least save the executables).
  530.  
  531. 1.5.  Script Generates Makefiles for PCCTS Projects
  532.  
  533.      A C program called genmk.c is available in the support  directory
  534. of the PCCTS release which has the following usage:
  535.  
  536. genmk project f1.g f2.g ... fn.g
  537.  
  538. It generates a makefile that creates an executable,  project,  from  a
  539. set of grammar files.  For example, genmk t t.g generates:
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.                                                                 Page 9
  558.  
  559.                                                                  PCCTS
  560.  
  561.  
  562.  
  563. #
  564. # PCCTS makefile for: t.g
  565. #
  566. DLG_FILE = parser.dlg
  567. ERR_FILE = err.c
  568. HDR_FILE = stdpccts.h
  569. TOK_FILE = tokens.h
  570. K = 1
  571. ANTLR_H = .
  572. BIN = .
  573. ANTLR = $(BIN)/antlr
  574. DLG = $(BIN)/dlg
  575. CFLAGS = -I. -I$(ANTLR_H)
  576. AFLAGS = -fe err.c -fh stdpccts.h -fl parser.dlg -ft tokens.h -k $(K)
  577. -gk
  578. DFLAGS = -C2 -i
  579. GRM = t.g
  580. SRC = scan.c t.c err.c
  581. OBJ = scan.o t.o err.o
  582.  
  583.  
  584.  
  585. t: $(OBJ) $(SRC)
  586.         cc -o t $(CFLAGS) $(OBJ)
  587.  
  588. t.c parser.dlg : t.g
  589.         $(ANTLR) $(AFLAGS) t.g
  590.  
  591. scan.c : parser.dlg
  592.         $(DLG) $(DFLAGS) parser.dlg scan.c
  593.  
  594.  
  595. This program is handy when beginning a new PCCTS project or when first
  596. learning about PCCTS.
  597.  
  598. 1.6.  DLG Supports Case Insensitive Scanners
  599.  
  600.      DLG has two new options which provide control over the case  sen-
  601. sitivity  of  the generated scanner.  Specifically, case insensitivity
  602. implies that when a character is referenced in a  regular  expression,
  603. DLG  behaves  as  if the user had typed both upper and lower case ver-
  604. sions of that character; i.e. (a|A) where a is  some  character.   The
  605. new options are:
  606.  
  607. -ci  Make lexical analyzer case insensitive
  608.  
  609. -cs  Make lexical analyzer case sensitive (default).
  610.  
  611. 1.7.  Delayed Lookahead Fetches in Generated Parser
  612.  
  613.      Currently, PCCTS generates parsers which always have k tokens  of
  614. lookahead.   This is done by following the strategy that another token
  615. is fetched (zzCONSUME) when one is matched (zzmatch).  This can  be  a
  616.  
  617.  
  618.  
  619.                                                                Page 10
  620.  
  621.                                                                  PCCTS
  622.  
  623.  
  624. problem for actions that need to occur after a token has been matched,
  625. but before the next token of lookahead is fetched.  This  is  somewhat
  626. overcome  in  PCCTS  1.00  by delaying the fetch if an action is found
  627. immediately after a token reference.  With the new  delayed  lookahead
  628. scheme,  the  next  token  is  not  consumed  until  the next match is
  629. required.  This means that any  action  before  the  next  match  (not
  630. necessarily  adjacent to the previous match) will be executed before a
  631. lookahead fetch occurs.  Turn on ANTLR option -gk and DLG option -i to
  632. enable  this  feature.   This  feature  appears to work with AST's and
  633. attributes with the constraints mentioned below in the  incompatibili-
  634. ties  section  (e.g.  use of LA(i) and LATEXT(i) has been restricted).
  635. It has been tested with the C (k>1 and Pascal (k==1) examples provided
  636. in the release and with several other large grammars.
  637.  
  638.      This feature is primarily useful for  developers  of  interactive
  639. tools.   Previously, it was really hard to get PCCTS to generate truly
  640. interactive tools.  It appeared as if the parser was always waiting on
  641. a  token  fetch  rather than executing an appropriate action.  E.g. in
  642. PCCTS 1.00,
  643.  
  644.  
  645. a : ( "A" "B" "C" "\n" ) <<printf("got it\n");>>
  646.   ;
  647.  
  648.  
  649. would not print got it until one token  after  the  newline  had  been
  650. typed.   PCCTS  1.06  will  generate  parsers  that  print the message
  651. immediately upon newline and will exit  without  waiting  for  another
  652. token as there are no token references following the action.
  653.  
  654.      Another way in which delayed lookahead is useful lies in transla-
  655. tors which add symbols to the symbol table which must be examined by a
  656. lexical action.  If a lookahead fetch occurs  too  fast,  the  lexical
  657. action may miss the introduction of a symbol into the symbol table.
  658.  
  659.      This feature is a bit flaky for the  moment-LA(i)  and  LATEXT(i)
  660. will  generally not have the same values as when the -gk option is not
  661. used (for k>=2).  Use attributes to access token values; the lookahead
  662. buffer  is not really a user object.  If you insist upon accessing the
  663. lookahead buffer, use LA(0) and LATEXT(0), which typically access  the
  664. last  token  matched  and last text matched respectively; this is dis-
  665. tinguished from LA(1),  which  means  the  next  token  of  lookahead.
  666. Accessing  the  next token of lookahead is invalid because it will not
  667. be fetched from the input stream until needed (just  before  the  next
  668. decision or match).
  669.  
  670. 1.8.  Tutorials Available
  671.  
  672.      With release 1.06, we  are  distributing  both  a  beginning  and
  673. advanced  tutorial.  They have not been thoroughly "debugged" much are
  674. much better than nothing:
  675.  
  676. Beginning
  677.      This tutorial introduces the  basic  functionality  of  PCCTS  by
  678.  
  679.  
  680.  
  681.                                                                Page 11
  682.  
  683.                                                                  PCCTS
  684.  
  685.  
  686.      example.   The  user  need not be familiar with parsing theory or
  687.      other compiler tools, but any familiarity  reduces  the  learning
  688.      curve substantially.
  689.  
  690. Advanced
  691.      Constructing a translator can be viewed as an  iterative  refine-
  692.      ment  process  moving  from language recognition to intermediate-
  693.      form  transformation.   This  tutorial  presents   one   possible
  694.      sequence of refinements.  It uses as many features of PCCTS as is
  695.      reasonable without regards to optimality.  It develops a compiler
  696.      for a simple string manipulation language called sc.  The result-
  697.      ing compiler generates code for a simple stack machine.
  698.  
  699. 1.9.  Error Messages for k>1
  700.  
  701.      Previous versions of PCCTS did not handle error message correctly
  702. for  k>1.  For example, with two tokens of lookahead and the following
  703. grammar:
  704.  
  705.  
  706. a : "A" "B" "D"
  707.   | "A" "C" "E"
  708.   ;
  709.  
  710.  
  711. an incorrect input of A D D would yield:
  712.  
  713.  
  714. line 1: syntax error at "A" missing A
  715.  
  716.  
  717. which is wrong (and incredibly confusing).  The  new  error  mechanism
  718. generates the following error message upon the same incorrect input:
  719.  
  720.  
  721. line 1: syntax error at "A D"; "D" not in { B C }
  722.  
  723.  
  724. which is infinitely superior.   Unfortunately,  situations  may  arise
  725. when  even  this  method will give an invalid message.  This may occur
  726. when alternatives have lookahead sequences which are  permutations  of
  727. the same tokens.
  728.  
  729.      The definition of the standard error reporting function,  zzsyn()
  730. has been modified.  The parameter list is now:
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.                                                                Page 12
  744.  
  745.                                                                  PCCTS
  746.  
  747.  
  748.  
  749. void
  750. zzsyn(char *text,
  751.       int tok,
  752.       char *egroup,
  753.       unsigned *eset,
  754.       int etok,
  755.       int k,
  756.       char *bad_text);
  757.  
  758.  
  759. Users can ignore this as it is transparent to them; unless, of course,
  760. the standard error reporting must be modified.  In addition, zzFAIL is
  761. now a function rather than a macro.
  762.  
  763. 1.10.  Trace Facility has Exit Macro
  764.  
  765.      Previously, only an entry trace macro  was  inserted  in  parsers
  766. when  the  -gd  ANTLR option was used.  An exit macro has been defined
  767. which resulted in zzTRACE becoming zzTRACEIN.  Also, a  default  trace
  768. macro  prints  out  "enter rule rule"  if  no default trace macros are
  769. defined.  To define your own, the macro definitions must appear in the
  770. #header action.  As before, the sole argument to the trace routines is
  771. a string representing the rule which has been entered or is  about  to
  772. be exited.
  773.  
  774. 1.11.  Resource Limitation
  775.  
  776.      Occasionally, ANTLR is unable to analyze a grammar  submitted  by
  777. the  user.   This  rare  situation  can only occur when the grammar is
  778. large and the amount of lookahead is greater than  one.   A  nonlinear
  779. analysis  algorithm  is  used  by  PCCTS to handle the general case of
  780. LL(k) parsing.  The average complexity of analysis, however,  is  near
  781. linear  due to some fancy footwork in the implementation which reduces
  782. the number of calls to the full LL(k) algorithm.
  783.  
  784.      To avoid the situation where ANTLR takes 23 hours of CPU time and
  785. then  runs  out of virtual memory, use the -rl n resource limit option
  786. where n is the maximum number of tree nodes to be used by the analysis
  787. algorithm.   An  error  message  will  be  displayed, if this limit is
  788. reached, which indicates which grammar construct  was  being  analyzed
  789. when  ANTLR hit a non-linearity.  Use this option if ANTLR seems to go
  790. off to lunch and your disk start swapping; try n=10000 to start.  Once
  791. the  offending  construct has been identified, try to remove the ambi-
  792. guity that ANTLR was trying to overcome with large lookahead analysis.
  793. Future  versions  of PCCTS may incorporate a known algorithm that does
  794. not exhibit this exponential behavior.
  795.  
  796. 1.12.  Rule Prefix Option
  797.  
  798.      An ANTLR option  has  been  added  that  prefixes  all  functions
  799. corresponding  to  rules  with  a prefix.  This can be used to provide
  800. symbol hiding in your project to isolate the parser.  It can  also  be
  801. used  to allow rule names that correspond to C keywords such as if and
  802.  
  803.  
  804.  
  805.                                                                Page 13
  806.  
  807.                                                                  PCCTS
  808.  
  809.  
  810. typedef.
  811.  
  812. 1.13.  Standard PCCTS Header
  813.  
  814.      Two new ANTLR options have been added that control  the  creation
  815. of  a  standard  PCCTS header file - stdpccts.h.  Option -gh instructs
  816. ANTLR to create a file, stdpccts.h unless -fh is used, which  contains
  817. all  header  information needed by non-PCCTS generated files that want
  818. to access PCCTS symbols.  For example,  it  indicates  the  k  of  the
  819. parser,  whether  trees are being constructed, whether lookahead is to
  820. be delayed, and indicates what  the  user  specified  in  the  #header
  821. action in the grammar file.  Previously, the user had to manually con-
  822. struct this information from the grammar file in order  to  place  the
  823. information  in  a non-PCCTS C file.  The -fh option is merely used to
  824. rename stdpccts.h.
  825.  
  826. 1.14.  Doubly-Linked AST's
  827.  
  828.      A new function is available in ast.c which will  doubly-link  any
  829. tree  that  is  passed  in.   To use this option, the user must define
  830. zzAST_DOUBLE in the #header directive or on the command-line of the  C
  831. compile.   This  defines  left  and up fields automatically in the AST
  832. node typedef.   ANTLR  generated  parsers,  normally,  only  construct
  833. singly-linked  trees.  The fields can be filled in via code similar to
  834. the following:
  835.  
  836.  
  837. #header <<
  838. #define AST_FIELDS      user-fields;
  839. >>
  840.  
  841. <<
  842. main()
  843. {
  844.         AST *root = NULL;
  845.  
  846.         ANTLR(start(&root), stdin);
  847.         zzdouble_link(root, NULL, NULL);
  848. }
  849.  
  850.  
  851. where the function is defined as:
  852.  
  853.  
  854. zzdouble_link(AST *t, AST *left, AST *up);
  855.  
  856.  
  857. 1.15.  C++ Compatible Parsers
  858.  
  859.      PCCTS parsers may now be compiled with C++  compilers;  i.e.  the
  860. output is more ANSI C-like than before.  It has been successfully com-
  861. piled with GCC 2.2, but not with GCC 1.37.  We do not  guarantee  any-
  862. thing.   To  be safe, use the -ga option so that PCCTS generates ANSI-
  863. style prototypes for functions generated  from  rules.   As  a  simple
  864.  
  865.  
  866.  
  867.                                                                Page 14
  868.  
  869.                                                                  PCCTS
  870.  
  871.  
  872. example, consider:
  873.  
  874.  
  875. #header <<
  876. #include "charbuf.h"
  877. /* stdio.h is included, by default, but doesn't seem to bother stream.h */
  878. #include <stream.h>
  879. #include <stdlib.h>
  880. >>
  881.  
  882. #token "[\ \t\n]" <<zzskip();>>
  883.  
  884. <<
  885. main()
  886. {
  887.         ANTLR(a(), stdin);
  888.         cout << "end of C++ test\n";
  889. }
  890. >>
  891.  
  892. a       :       "A" "B" << cout << $1.text << $2.text << "\n"; >>
  893.         ;
  894.  
  895.  
  896. which does not do much:
  897.  
  898.  
  899. % t
  900. A B
  901. AB
  902. end of C++ test
  903. %
  904.  
  905.  
  906. but it does compile with G++ 2.2 except that a  warning  is  generated
  907. concerning  strncpy()  not being declared before use.  This is trivial
  908. to fix, of course - by modifying the charbuf.h file.  We compiled this
  909. with:
  910.  
  911.  
  912. antlr -k 2 -gk -ga t.g
  913. Antlr parser generator   Version 1.06   1989-1992
  914. dlg -C2 -i parser.dlg scan.c
  915. dlg  Version 1.06   1989-1992
  916. g++ -I. -I../1.06/h -g -c scan.c
  917. g++ -I. -I../1.06/h -g -c t.c
  918. t.c: In function `struct Attrib zzconstr_attr (int, char *)':
  919. t.c:19: warning: implicit declaration of function `strncpy'
  920. g++ -I. -I../1.06/h -g -c err.c
  921. g++ -o t -I. -I../1.06/h -g scan.o t.o err.o
  922.  
  923.  
  924. We anticipate a rewrite to be more C++ sometime in the future.
  925.  
  926.  
  927.  
  928.  
  929.                                                                Page 15
  930.  
  931.                                                                  PCCTS
  932.  
  933.  
  934. 2.  Acknowledgements
  935.  
  936.      We acknowledge Dan Lawrence of MDBS for the new  error  reporting
  937. facility concerning greater than one token of lookahead; Dana Hoggatt,
  938. also of MDBS, suggested the rule prefix idea  (-gp  option)  and  beta
  939. tested   1.06.   We  thank  Ed  Harfmann  of  MDBS  for  creating  the
  940. makefile.os2 files and porting it to the PC.  We acknowledge the  fol-
  941. lowing  beta  testers  for  1.06  (alphabetically):   Thomas  Buehlman
  942. (buehlman@iwf.mabp.ethz.ch),  Peter  Dahl   (dahl@everest.ee.umn.edu),
  943. Chris  Song (dsong@ncsa.uiuc.edu), Ariel Tamches (tamches@cs.UMD.EDU).
  944. We reference Russell Quong (quong@ecn.purdue.edu) of Purdue EE for his
  945. work  with  us  on  defining  and  refining predicates.  Ariel Tamches
  946. (tamches@cs.UMD.EDU) deserves attention for hacking on the particulars
  947. of the alpha-release predicates.
  948.  
  949. 3.  Machine Compatibility
  950.  
  951.      PCCTS Version 1.06 has been tested on the following platforms:
  952.  
  953. Sun 3/60
  954.  
  955. Sun SPARC I, II
  956.  
  957. Encore Multimax running Umax 4.3
  958.  
  959. Sun sparcstation IPX
  960.  
  961. NeXTstation
  962.  
  963. Decstation 3100 running Ultrix 4.2
  964.  
  965. DEC 5000
  966.  
  967. Linux on 386PC
  968.  
  969. Microsoft C 6.0 on PC OS/2, DOS
  970.  
  971. CSET/2 C compiler on PC OS/2
  972.  
  973. IBM RS6000
  974.  
  975. MIPS r2000
  976.  
  977. 4.  Incompatibilities
  978.  
  979.      Due to the bug fixes in 1.06, "new"  ambiguities  may  appear  in
  980. your grammar.  These were always there-ANTLR simply did not find them.
  981. The analysis is more correct.
  982.  
  983.      Calls to zzTRACE are no longer generated by the -gd option.  Now,
  984. zzTRACEIN,  zzTRACEOUT  are  called  at the beginning and end of func-
  985. tions, respectively.
  986.  
  987.      The way in which  PCCTS  translates  actions  has  been  changed;
  988.  
  989.  
  990.  
  991.                                                                Page 16
  992.  
  993.                                                                  PCCTS
  994.  
  995.  
  996. before  they were parsed with a C function, now the #lexclass facility
  997. is being used.  Some differences in  translation  may  be  discovered;
  998. e.g. a character may need to be escaped with \ whereas before the sim-
  999. ple character was sufficient.
  1000.  
  1001.      The user should no longer set the next token of lookahead or  the
  1002. text  of  the  next  token  in  the  lexical  analyzer using LA(1) and
  1003. LATEXT(1).  This is incompatible with the -gk option; hence,  NLA  and
  1004. NLATEXT should be used instead where the N means "next".
  1005.  
  1006.      The -ga does not generate anything different as the code  genera-
  1007. tor now dumps both K&R and ANSI with #ifdef's around the ANSI code.
  1008.  
  1009.      Previously, no prototype was given when -ga was off.  Now, proto-
  1010. types  are  always  generated (with the appropriated #ifdef's).  These
  1011. prototypes can conflict with the outside environment if the rule names
  1012. are  things  like  if  and  stat  (which  is  a system call).  Use the
  1013. -gp prefix option to prefix all functions corresponding to rules  with
  1014. a string of your choice.
  1015.  
  1016. 5.  Future Work:
  1017.  
  1018.      Predicates are still under development.  We  expect  an  enhanced
  1019. version of PCCTS that computes context and hoists more aggressively.
  1020.  
  1021.      Often a grammar construct cannot be left factored  to  remove  an
  1022. ambiguity.   This  typically  arises  in the situation that the common
  1023. prefix can be arbitrarily long.  Fortunately, input is typically  fin-
  1024. ite  and  one could scan past these constructs given enough lookahead.
  1025. This is not the same thing as backtracking as the parser  never  backs
  1026. up;  it  simply  looks  ahead really far to make a decision.  This can
  1027. easily be handled with a predicate of the form:
  1028.  
  1029.  
  1030. <<is_this_found_ahead(context)>>?
  1031.  
  1032.  
  1033. which would look ahead in the lookahead buffer to see if  the  context
  1034. occurred  within  some  finite number of tokens.  This concept is very
  1035. similar to LR-Regular (LRR) parsers for those  familiar  with  parsing
  1036. theory.   Note  that  this  is a very fast, cheap way to get something
  1037. resembling the power of backtracking.
  1038.  
  1039.      Attribute names  are  expected  to  be  enhanced.   For  example,
  1040. instead of $i, $token_name could be used:
  1041.  
  1042.  
  1043. a : WORD TYPE <<printf("%s %s\n", $WORD, $TYPE);>>
  1044.   ;
  1045.  
  1046.  
  1047.      We expect to have a graphical user interface to PCCTS sometime in
  1048. the  future  which allows entry of grammars using syntax diagram nota-
  1049. tion.  The interface is expected to run under X Windows.
  1050.  
  1051.  
  1052.  
  1053.                                                                Page 17
  1054.  
  1055.                                                                  PCCTS
  1056.  
  1057.  
  1058.      We anticipate a version that supports object-oriented programming
  1059. and  generates C++ instead of ANSI C.  For the moment, PCCTS is compa-
  1060. tible with C++, but future versions will support C++.
  1061.  
  1062.      Future versions, both C and C++, will be able to refer  to  PCCTS
  1063. symbols  by  pccts.symbol  instead  of  zzsymbol.   E.g. zzskip() will
  1064. become pccts.skip().
  1065.  
  1066.      DLG will soon use lookahead of its own to allow  the  recognition
  1067. of  more  complicated expressions; specifically, those which have left
  1068. substrings in common with other regular expressions.
  1069.  
  1070.      We expect future versions of PCCTS to dump grammar  analysis  and
  1071. parser construction statistics such as how many rules required 1 token
  1072. of lookahead, how many ambiguities etc...
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.                                                                Page 18
  1116.  
  1117.